﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;
using FinPlusAssembler;
using FinPlusInterfaces;

namespace FinPlusCompView
{
    public partial class FinPlusColValueShiftBar : FinPlusControl
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }
        [State(Save = true, Test = true)]
        public bool Hidden { get { return (bool)shiftsToggle.IsChecked; } set { shiftsToggle.IsChecked = value; ColShiftsHide(value); } }
        public IDictionary<string, string> ValueShiftColumns { get { return _valueShiftColumns; } }
        public event EventHandler<IPod> ColValueShift;

        private IDictionary<string, string> _valueShiftColumns = new Dictionary<string, string>();

        //construct
        public FinPlusColValueShiftBar(string caption, string shifts)
        {
            InitializeComponent();
            shiftsToggle.Content = caption;
            foreach (var shift in shifts.Split(';').ToList<string>())
                AddButton(shift);

            IsValid = true;
        }

        //common control interface
        public override void CmdExecute(IPod command)
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.ColValueShiftUp: 
                    case Cmds.ColValueShiftDown:
                    case Cmds.ColValShiftLevel: ColValueShifts(command); break;
                    case Cmds.ColShiftsHide: ColShiftsHide(bool.Parse(command["Tag"].ToString())); break;
                    default: throw new Exception(string.Format("cmd not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                IsValid = Level.Warning.Log(Id, Config, e.Message, e);
            }
        }

        //private
        private void AddButton(string shift)
        {
            var button = new FinPlusColValueShiftButton(shift);
            _valueShiftColumns[button.Id] = shift;
            colShiftBar.Children.Add(button);
            button.ColShift += ColShift_Click;
        }

        private void ColValueShifts(IPod pod)
        {
            ColValueShift.Invoke(this, pod);
        }

        private void ColShiftsHide(bool hidden)
        {
            if (hidden)
            {
                colShiftBar.Width = 0;
                colShiftBar.IsEnabled = false;
            }
            else
            {
                colShiftBar.Width = Double.NaN;
                colShiftBar.IsEnabled = true;
            }
        }

        //cmds
        private enum Cmds { ColValueShiftUp, ColValueShiftDown, ColValShiftLevel, ColShiftsHide }

        //events
        private void ColShift_Click(object s, IPod f) { CmdExecute(f); } //ColValueShiftUp, ColValueShiftDown, ColValShiftLevel
        private void ColShiftsHide_Click(object s, RoutedEventArgs a) { CmdRouter(Cmds.ColShiftsHide.ToString(), ((ToggleButton)s).IsChecked.ToString()); }
    }
}
